<?php require_once('../../Connections/cotiza.php'); ?>
<?php
$campo  = $_GET['campo'];
$campo1 = $_GET['campo1'];
$flag   = $_GET['flag'];
$label  = $_GET['label'];
$tprod  = $_GET['tprod'];
$filtro = $_GET['filtro'];

$acc   = $_GET['acc'];
mysql_select_db($database_cotiza, $cotiza);

if($acc == "search")
{
	$b = $_POST['busqueda'];
	if($filtro != "")
		$query = "SELECT mat_mstr.mat_id, mat_mstr.mat_desc, mat_mstr.mat_tipo, defruta_det.defruta_fam_id, fam_mstr.fam_desc
				  FROM defruta_det Inner Join mat_mstr ON mat_mstr.mat_id = defruta_det.defruta_mat_id
                  Inner Join fam_mstr ON defruta_det.defruta_fam_id = fam_mstr.fam_id
				  WHERE defruta_tipo_id = '$filtro' AND (mat_id like '%$b%' or mat_desc like '%$b%') ORDER BY mat_tipo, mat_desc";
	else
		$query = "SELECT * FROM mat_mstr WHERE mat_id like '%$b%' or mat_desc like '%$b%' ORDER BY mat_tipo, mat_desc";
}
else
{
	if($filtro != "")
		$query = "SELECT mat_mstr.mat_id, mat_mstr.mat_desc, mat_mstr.mat_tipo, defruta_det.defruta_fam_id, fam_mstr.fam_desc
				  FROM defruta_det Inner Join mat_mstr ON mat_mstr.mat_id = defruta_det.defruta_mat_id
				  Inner Join fam_mstr ON defruta_det.defruta_fam_id = fam_mstr.fam_id
				  WHERE defruta_tipo_id =  '$filtro' ORDER BY mat_tipo, mat_desc";
	else
		$query = "SELECT * FROM mat_mstr ORDER BY mat_tipo, mat_desc";
}
$busqueda = mysql_query($query, $cotiza) or die(mysql_error());
$row = mysql_fetch_assoc($busqueda);
$totalRows = mysql_num_rows($busqueda);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>..:: Materiales ::..</title>
<link href="../../main.css" rel="stylesheet" type="text/css" />
<script>
function retornar(cod,desc,ruta,ruta_des,laminas)
{
	var campo   = '<?php echo $campo;  ?>';
	var familia = '<?php echo $row['fam_desc'];  ?>'
	window.opener.document.getElementById(campo).value= cod;
	window.opener.document.getElementById(campo).focus();
	<?php if($label  <> "") echo "window.opener.document.getElementById('$label').innerHTML = desc;"; ?>
	<?php //if($tprod  <> "") echo "window.opener.document.getElementById('ruta').value = ruta;"; ?>
	<?php // if($filtro  <> "") echo "window.opener.document.getElementById('detail-ruta').innerHTML = ruta_des;"; ?>
	<?php if($filtro <> "") echo "window.opener.document.getElementById('detail-familia').innerHTML = '<strong>' + ruta + ' - ' + ruta_des + '</strong>';"; ?>
	//alert(laminas);
	window.close();	
}

</script>
<style type="text/css">
<!--
.default_title {
	color: #FFF;
}
-->
</style>
</head>

<body onload="document.form1.busqueda.focus();">
<form id="form1" name="form1" method="post" action="materiales.php?filtro=<?php echo $filtro;?>&campo=<?php echo $campo; ?>&acc=search&label=<?php echo $label;?>">
  <table border="0" align="center">
    <tr>
      <td >B&uacute;squeda de Material:</td>
      <td ><input name="busqueda" class="default" size="20" autocomplete="off" value="<?php echo $b;?>"/></td>
      <td ><input type="submit" value="Buscar" class="v8" /></td>
    </tr>
  </table>
</form>
<br />
<?php if($totalRows >0){?>
  <table width="460" border="1" align="center" cellpadding="2" cellspacing="0">
    <tr class="default_title">
      <td width="137" align="center" bgcolor="#0000CC">C&oacute;digo</td>
      <td width="309" align="center" bgcolor="#0000CC">Descripci&oacute;n</td>
    </tr>
    <?php do { 
		//if($tprod <> "")
		//{
			$mid = $row['mat_id'];
			$q = "SELECT defruta_fam_id as ruta FROM defruta_det WHERE defruta_tipo_id = '$tprod' AND defruta_mat_id = '$mid'";
			$res = mysql_query($q,$cotiza) or die (mysql_error());
			$r = mysql_fetch_assoc($res);
			$ruta = $row['defruta_fam_id'];
			
			$q  = "SELECT fam_desc FROM fam_mstr WHERE fam_id = '$ruta'";
			$re = mysql_query($q,$cotiza) or die (mysql_error());
			$rs = mysql_fetch_assoc($re);
			$desc_ruta = $rs['fam_desc'];
			
			if($row['mat_tipo'] == "L")
				$laminas = substr_count($row['mat_id'],"/") + 1;
			else
				$laminas = 0;
		//}
	?>
    <tr>
      <td width="137"><a href="javascript:retornar('<?php echo $row['mat_id']."','".$row['mat_desc']."','".$ruta."','".$desc_ruta."','".$laminas; ?>');"> <?php echo $row['mat_id']; ?> </a>&nbsp;</td>
      <td width="309"><?php echo $row['mat_desc']; ?>&nbsp;</td>
    </tr>
      <?php } while ($row = mysql_fetch_assoc($busqueda)); ?>
  </table>
<?php }?>

<?php if($totalRows == 0){?>
<table width="378" border="0" align="center">
  <tr>
    <td width="372" align="center"><strong>No existen materiales disponibles</strong></td>
  </tr>
</table>
<?php }?>
</body>
</html>
<?php 
mysql_close($cotiza);
?>